package com.yhsoft.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.sys.controller.Response.BaicContentByFwxyOutVo;
import com.yhsoft.sys.controller.Response.BaicContentByHomeOutVo;
import com.yhsoft.sys.controller.Response.BaicContentByYszcOutVo;
import com.yhsoft.sys.controller.Response.BaicContentOutVo;
import com.yhsoft.sys.controller.request.BaicContentInVo;
import com.yhsoft.sys.controller.request.BaicInContentByWXInVo;
import com.yhsoft.sys.entity.BaicContent;
import com.yhsoft.sys.entity.BaicInterfaceConfig;
import com.yhsoft.sys.mapper.BaicContentMapper;
import com.yhsoft.sys.mapper.BaicInterfaceConfigMapper;
import com.yhsoft.sys.service.IBaicContentService;
import com.yhsoft.sys.service.IBaicInterfaceConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 内容配置表 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-10-10
 */
@Service
public class BaicContentServiceImpl extends ServiceImpl<BaicContentMapper, BaicContent> implements IBaicContentService {

    @Resource
    private BaicInterfaceConfigMapper baicInterfaceConfigMapper;

    @Override
    public void addBaicContent(BaicContentInVo inVo) {
        BaicContent content = new BaicContent();
        BeanUtils.copyProperties(inVo, content);
        content.setCompanyId(TokenService.getUserBytoken().getCompanyId());

        //通过companyid查询appid
        LambdaQueryWrapper<BaicInterfaceConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaicInterfaceConfig::getCompanyId, content.getCompanyId());
        BaicInterfaceConfig baicInterfaceConfig = baicInterfaceConfigMapper.selectOne(wrapper);

        content.setAppId(baicInterfaceConfig.getAppId());

        this.baseMapper.insert(content);
    }

    @Override
    public void updateBaicContent(BaicContentInVo inVo) {
        BaicContent content = new BaicContent();
        BeanUtils.copyProperties(inVo, content);
        this.baseMapper.updateById(content);
    }

    @Override
    public BaicContentOutVo getAllBaicContentByPC() {
        LambdaQueryWrapper<BaicContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaicContent::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        BaicContent allBaicContentById = this.baseMapper.getAllBaicContentById(queryWrapper);
        return BeanUtil.copyProperties(allBaicContentById, BaicContentOutVo.class);
    }

    @Override
    public BaicContentOutVo getAllBaicContentByWX(String appId) {
        LambdaQueryWrapper<BaicContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaicContent::getAppId, appId);
        BaicContent baicContent = this.baseMapper.getAllBaicContentById(queryWrapper);
        return BeanUtil.copyProperties(baicContent, BaicContentOutVo.class);
    }

    @Override
    public BaicContentByFwxyOutVo getFwxyByWX(String appId) {
        LambdaQueryWrapper<BaicContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaicContent::getAppId, appId);
        BaicContent baicContent = this.baseMapper.getAllBaicContentById(queryWrapper);
        return BeanUtil.copyProperties(baicContent, BaicContentByFwxyOutVo.class);
    }

    @Override
    public BaicContentByYszcOutVo getYszcByWX(String appId) {
        LambdaQueryWrapper<BaicContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaicContent::getAppId, appId);
        BaicContent baicContent = this.baseMapper.getAllBaicContentById(queryWrapper);
        return BeanUtil.copyProperties(baicContent, BaicContentByYszcOutVo.class);
    }

    @Override
    public BaicContentByHomeOutVo getHomeContentByWX(String appId) {
        LambdaQueryWrapper<BaicContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaicContent::getAppId, appId);
        BaicContent baicContent = this.baseMapper.getAllBaicContentById(queryWrapper);
        return BeanUtil.copyProperties(baicContent, BaicContentByHomeOutVo.class);
    }

    @Override
    public BaicContent getThisBaicContent() {
        LambdaQueryWrapper<BaicContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaicContent::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        return this.baseMapper.getAllBaicContentById(queryWrapper);
    }
}
